package com.ruoyi.web.exams.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.exams.domain.ExamPager;
import com.ruoyi.web.exams.domain.ExamPagerQuestions;
import com.ruoyi.web.exams.domain.ExamQuestion;
import com.ruoyi.web.exams.dto.PagerQuestionsBatchVo;
import com.ruoyi.web.exams.mapper.ExamPagerMapper;
import com.ruoyi.web.exams.mapper.ExamPagerQuestionsMapper;
import com.ruoyi.web.exams.mapper.ExamQuestionMapper;
import com.ruoyi.web.exams.service.IExamPagerQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 试卷题目关联Service业务层处理
 *
 * @author 范书铭
 * @date 2024-10-02
 */
@Service
public class ExamPagerQuestionsServiceImpl extends ServiceImpl<ExamPagerQuestionsMapper, ExamPagerQuestions> implements IExamPagerQuestionsService {
    @Autowired
    private ExamPagerQuestionsMapper examPagerQuestionsMapper;
    @Resource
    ExamPagerMapper examPagerMapper;
    @Resource
    ExamQuestionMapper examQuestionMapper;

    /**
     * 查询试卷题目关联
     *
     * @param id 试卷题目关联主键
     * @return 试卷题目关联
     */
    @Override
    public ExamPagerQuestions selectExamPagerQuestionsById(Long id) {
        return examPagerQuestionsMapper.selectExamPagerQuestionsById(id);
    }

    /**
     * 查询试卷题目关联列表
     *
     * @param examPagerQuestions 试卷题目关联
     * @return 试卷题目关联
     */
    @Override
    public List<ExamPagerQuestions> selectExamPagerQuestionsList(ExamPagerQuestions examPagerQuestions) {
        return examPagerQuestionsMapper.selectExamPagerQuestionsList(examPagerQuestions);
    }

    /**
     * 新增试卷题目关联
     *
     * @param examPagerQuestions 试卷题目关联
     * @return 结果
     */
    @Override
    public int insertExamPagerQuestions(ExamPagerQuestions examPagerQuestions) {
        examPagerQuestions.setCreateTime(DateUtils.getNowDate());
        return examPagerQuestionsMapper.insertExamPagerQuestions(examPagerQuestions);
    }

    /**
     * 修改试卷题目关联
     *
     * @param examPagerQuestions 试卷题目关联
     * @return 结果
     */
    @Override
    public int updateExamPagerQuestions(ExamPagerQuestions examPagerQuestions) {
        examPagerQuestions.setUpdateTime(DateUtils.getNowDate());
        return examPagerQuestionsMapper.updateExamPagerQuestions(examPagerQuestions);
    }

    /**
     * 批量删除试卷题目关联
     *
     * @param ids 需要删除的试卷题目关联主键
     * @return 结果
     */
    @Override
    public int deleteExamPagerQuestionsByIds(Long[] ids) {
        return examPagerQuestionsMapper.deleteExamPagerQuestionsByIds(ids);
    }

    /**
     * 删除试卷题目关联信息
     *
     * @param id 试卷题目关联主键
     * @return 结果
     */
    @Override
    public int deleteExamPagerQuestionsById(Long id) {
        return examPagerQuestionsMapper.deleteExamPagerQuestionsById(id);
    }

    @Transactional
    @Override
    public boolean addQuestionsBatch(PagerQuestionsBatchVo examPagerQuestions) {
        ExamPager examPager = examPagerMapper.selectById(examPagerQuestions.getPackageId());
        Assert.notNull(examPager, "试卷ID错误");
        //查出所有的题目列表
        List<ExamQuestion> questionsAdded = examQuestionMapper.selectList(new QueryWrapper<ExamQuestion>().in("id", examPagerQuestions.getQuestionId()));
        if (examPager.getPagerType() == 3) {
            //模拟试卷,需要校对分数,并且覆盖原有的题目
            //删除已经关联的题目
            remove(new QueryWrapper<ExamPagerQuestions>().eq("package_id", examPagerQuestions.getQuestionId()));
            Assert.isTrue(questionsAdded.size() == examPagerQuestions.getQuestionId().size(), "题目ID有错误,请勿违规操作");
            //分析题目数量是否符合设置
            int singleQuestionNumber = 0;
            int multipleQuestionNumber = 0;
            int judgeQuestionNumber = 0;

            for (ExamQuestion question : questionsAdded) {
                if (question.getQuestionType() == 1) {
                    singleQuestionNumber++;
                }
                if (question.getQuestionType() == 2) {
                    multipleQuestionNumber++;
                }
                if (question.getQuestionType() == 3) {
                    judgeQuestionNumber++;
                }
            }
            if (singleQuestionNumber != examPager.getSingleQuestionNumber()) {
                throw new BusinessException("单选题数量不对");
            }
            if (multipleQuestionNumber != examPager.getMultiQuestionNumber()) {
                throw new BusinessException("多选题数量不对");
            }
            if (judgeQuestionNumber != examPager.getJudgeQuestionNumber()) {
                throw new BusinessException("判断题数量不对");
            }
            List<ExamPagerQuestions> examPagerQuestionsList = new ArrayList<>();
            for (Long questionId : examPagerQuestions.getQuestionId()) {
                ExamPagerQuestions examPagerQuestions1 = new ExamPagerQuestions();
                examPagerQuestions1.setQuestionId(questionId);
                examPagerQuestions1.setPackageId(examPagerQuestions.getPackageId());
                examPagerQuestionsList.add(examPagerQuestions1);
            }
            return saveBatch(examPagerQuestionsList);
        } else {
            //练习模块,只去重,无限添加
            //查出所有试卷关联的题目
            List<ExamPagerQuestions> examPagerQuestionsListHas = list(new QueryWrapper<ExamPagerQuestions>().eq("packageId", examPagerQuestions.getQuestionId())
                    .in("id", examPagerQuestions.getQuestionId()));
            //挑出还没被加入的题目,存入新数据
            List<ExamPagerQuestions> examPagerQuestionsList = new ArrayList<>();
            for (Long questionId : examPagerQuestions.getQuestionId()) {
                boolean flag = false;
                for (ExamPagerQuestions examPagerQuestions1 : examPagerQuestionsListHas) {
                    if (examPagerQuestions1.getPackageId().longValue() == (questionId.longValue())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    ExamPagerQuestions examPagerQuestions1 = new ExamPagerQuestions();
                    examPagerQuestions1.setQuestionId(questionId);
                    examPagerQuestions1.setPackageId(examPagerQuestions.getPackageId());
                    examPagerQuestionsList.add(examPagerQuestions1);
                }
            }
            return saveBatch(examPagerQuestionsList);
        }
    }

    @Override
    public boolean deleteQuestions(PagerQuestionsBatchVo pagerQuestionsBatchVo) {
        ExamPager examPager = examPagerMapper.selectById(pagerQuestionsBatchVo.getPackageId());
        Assert.notNull(examPager, "试卷ID错误");
        Assert.isTrue(examPager.getPagerType() != 3, "模拟试卷请直接重新选题覆盖");
        return remove(new LambdaQueryWrapper<ExamPagerQuestions>().eq(ExamPagerQuestions::getPackageId, pagerQuestionsBatchVo.getQuestionId()));
    }
}